<?php

class AdministracionController extends Zend_Controller_Action
{
    
    public function init() {
        
    }
    
    public function indexAction(){
        return $this->_redirect('/administracion/bibliotecarios');
    }
    
    public function loginAction() {
        if(Zend_Auth::getInstance()->hasIdentity()) {  
            return $this->_redirect('/index/index');
        }
        
        $db = Zend_Db_Table::getDefaultAdapter();
        $Mod_B = new Model_Bibliotecarios();
        
        $form = new Form_login();
        $this->view->form = $form;
        
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($_POST)) {
                $data = $form->getValues();
                $auth = Zend_Auth::getInstance();  
                $authAdapter = new Zend_Auth_Adapter_DbTable($Mod_B->getAdapter(), $Mod_B->GetTblName());
                
                $authAdapter->setIdentityColumn('bibliotecario_login')
                            ->setCredentialColumn('bibliotecario_password');
                $authAdapter->setIdentity($data['username'])
                            ->setCredential(md5($data['password']));
                            //->setCredential($data['password']);
                
                $select = $authAdapter->getDbSelect();
                $select->where($Mod_B->GetTblName().".bibliotecario_bloqueado = 0");
                $result = $auth->authenticate($authAdapter);
                
                $frontController = Zend_Controller_Front::getInstance();
                require_once (MOD_PATH.'/plugins/AccessCheck.php');
                $registry = Zend_Registry::getInstance();
                $frontController->registerPlugin(new Plugin_AccessCheck($registry->get('acl')));

                if ($result->isValid()) {
                    $storage = new Zend_Auth_Storage_Session();
                    $storage->write($authAdapter->getResultRowObject());
                    
                    $obj_B = $Mod_B->GetBibliotecarioIdByLogin($data['username']);
                    $ses_id = session_id();
                    $ses_inicio = strtotime(date("d-m-Y H:i:s"));
                    $ses_bibliotecario = $obj_B[0]['bibliotecario_id'];

                    $obj_S = new Sesion($ses_id, $ses_inicio, $ses_bibliotecario);
                    $obj_S->Guardame();

                    $_SESSION['OBJ_SESION'] = $obj_S;
                    
                    return $this->_redirect('/index/index');
                } else {
                    $msg = "Usuario o contraseña no validos";
                    $msgType = "errMsg";
                    $this->view->messagebox = array('message' => $msg, 'status' => $msgType);
                }
            }
        }
    }
    
    public function logoutAction() {
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        $storage = new Zend_Auth_Storage_Session();
        $storage->clear();
        
        $obj_S = $_SESSION['OBJ_SESION'];
        $obj_S->Eliminame();
        unset($_SESSION['OBJ_SESION']);
        
        return $this->_redirect('/administracion/login');
    }

    public function bibliotecariosAction() {
        $form = new Form_bibliotecario();
        $search = new Form_search();
        $this->view->form = $form;
        $this->view->search = $search;
        
        $this->view->list_roles = Rol::GetRoles();

        $isedit = $this->_request->getParam('edit');
        if ($isedit){
            $bibliotecario_id = $this->_request->getParam('edit');
            $bibliotecario = Bibliotecario::GetInstancia($bibliotecario_id);
            
            $form->name->setValue($bibliotecario->nombre);
            $form->logname->setValue($bibliotecario->login);
            //$form->passwd->setValue(md5($bibliotecario->password));
            $form->passwd->setRequired(FALSE);
            $form->roleId->setValue($bibliotecario->rol_id);
            $form->chkbloqueado->setValue($bibliotecario->bloqueado);
            $form->hiddenedit->setValue($bibliotecario->id);
        }
        
        $isdelet = $this->_request->getParam('delet');
        if ($isdelet){
            $bibliotecario_id = $this->_request->getParam('delet');
            $bibliotecario = Bibliotecario::GetInstancia($bibliotecario_id);
            
            $result = $bibliotecario->Eliminame();
            if ($result) {
                return $this->_redirect('/administracion/bibliotecarios/msg/c1');
            } else {
                return $this->_redirect('/administracion/bibliotecarios/msg/c2');
            }
        }
        
        
        if ($this->getRequest()->isPost()) {
            if ($this->view->form->isValid($_POST)) {
                $bibliotecario = new Bibliotecario(
                        null,
                        $this->_request->getParam('name'),
                        $this->_request->getParam('logname'),
                        md5($this->_request->getParam('passwd')),
                        $this->_request->getParam('chkbloqueado'),
                        $this->_request->getParam('roleId')
                );
                
                $hiddenId = $this->_request->getParam('hiddenedit');
                if (!$hiddenId) {
                    $result = $bibliotecario->Guardame();
                    if ($result) {
                        return $this->_redirect('/administracion/bibliotecarios/msg/a1');
                    } else {
                        return $this->_redirect('/administracion/bibliotecarios/msg/a2');
                    }
                } else {
                    $result = $bibliotecario->Modificame($hiddenId);
                    if ($result) {
                        return $this->_redirect('/administracion/bibliotecarios/msg/b1');
                    } else {
                        return $this->_redirect('/administracion/bibliotecarios/msg/b2');
                    }
                }
            }
            if ($this->view->search->isValid($_POST)) {
                $searchword = $this->_request->getParam('searchword');
                $this->view->bibliotecarios = Bibliotecario::GetBibliotecarios($searchword);
            }
        } else {
            $this->view->bibliotecarios = Bibliotecario::GetBibliotecarios();
        }
    }
    
    public function rolesAction() {
        $form = new Form_roles();
        $search = new Form_search();
        $this->view->form = $form;
        $this->view->search = $search;

        $isedit = $this->_request->getParam('edit');
        if ($isedit){
            $rol_id = $this->_request->getParam('edit');
            $rol = Rol::GetInstancia($rol_id);
            
            $form->name->setValue($rol->nombre);
            $form->chkAdministracion->setValue($rol->administracion);
            $form->chkCatalogacion->setValue($rol->catalogacion);
            $form->chkCirculacion->setValue($rol->circulacion);
            $form->chkReportes->setValue($rol->reportes);
            $form->chkRespaldos->setValue($rol->respaldos);
            $form->hiddenedit->setValue($rol->id);
        }
        
        if ($this->getRequest()->isPost()) {
            if ($this->view->form->isValid($_POST)) {
                $rol = new Rol(
                        null, 
                        $this->_request->getParam('name'), 
                        $this->_request->getParam('chkAdministracion'),
                        $this->_request->getParam('chkCatalogacion'),
                        $this->_request->getParam('chkCirculacion'), 
                        $this->_request->getParam('chkReportes'),
                        $this->_request->getParam('chkRespaldos')
                );
                
                $hiddenId = $this->_request->getParam('hiddenedit');
                if (!$hiddenId) {
                    $result = $rol->Guardame();
                    if ($result) {
                        return $this->_redirect('/administracion/roles/msg/a1');
                    } else {
                        return $this->_redirect('/administracion/roles/msg/a2');
                    }
                } else {
                    $result = $rol->Modificame($hiddenId);
                    if ($result) {
                        return $this->_redirect('/administracion/roles/msg/b1');
                    } else {
                        return $this->_redirect('/administracion/roles/msg/b2');
                    }
                }
            }
            if ($this->view->search->isValid($_POST)) {
                $searchword = $this->_request->getParam('searchword');
                $this->view->roles = Rol::GetRoles($searchword);
            }
        } else {
            $this->view->roles = Rol::GetRoles();
        }
    }

    
}